package com.ruoyi.system.service.impl;

import com.ruoyi.system.service.IUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Service
@Slf4j
public class UploadServiceImpl implements IUploadService {
    private static final String BASE_PATH = "/www/wwwroot/1.95.145.238";
    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        String datePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Path targetDir = Paths.get(BASE_PATH, datePath); // 构建目录路径

        if (!Files.exists(targetDir)) {
            Files.createDirectories(targetDir); // 创建目录
        }

        // 生成唯一文件名
        String fileName = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
        Path targetPath = targetDir.resolve(fileName);

        // 保存文件
        Files.copy(file.getInputStream(), targetPath);
        log.info("Saving file to directory: {}", targetDir.toAbsolutePath());
        log.info("Full file path: {}", targetPath.toAbsolutePath());
        // 返回文件的 URL
        return String.format("http://1.95.145.238/%s/%s",datePath, fileName);
    }
}
